﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace CTManagerSoln
{
    public partial class Form4 : Form
    {
        ComboBox combobox1;
        public Form4()
        {
            InitializeComponent();
            
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("product_property");
            dt.Columns.Add("value");
            dt.Rows.Add("status","true");
            dt.Rows.Add("color","blue");
            dt.Rows.Add("date");

            this.dataGridView1.DataSource = dt;

            this.combobox1 = new ComboBox();
            this.combobox1.Visible = false;
            this.dataGridView1.Controls.Add(this.combobox1);

            this.dataGridView1.CellBeginEdit += new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);
            this.dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
            this.combobox1.KeyPress += new KeyPressEventHandler(combobox1_KeyPress);
            this.combobox1.SelectedIndexChanged += new EventHandler(combobox1_SelectedIndexChanged);
        }

        void combobox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        void combobox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.AutoComplete(this.combobox1, e);
        }
        public void AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e)
        {
            this.AutoComplete(cb, e, false);
        }

        public void AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e, bool blnLimitToList)
        {
            string strFindStr = "";

            if (e.KeyChar == (char)8)
            {
                if (cb.SelectionStart <= 1)
                {
                    cb.Text = "";
                    return;
                }

                if (cb.SelectionLength == 0)
                    strFindStr = cb.Text.Substring(0, cb.Text.Length - 1);
                else
                    strFindStr = cb.Text.Substring(0, cb.SelectionStart - 1);
            }
            else
            {
                if (cb.SelectionLength == 0)
                    strFindStr = cb.Text + e.KeyChar;
                else
                    strFindStr = cb.Text.Substring(0, cb.SelectionStart) + e.KeyChar;
            }

            int intIdx = -1;

            // Search the string in the ComboBox list.

            intIdx = cb.FindString(strFindStr);

            if (intIdx != -1)
            {
                cb.SelectedText = "";
                cb.SelectedIndex = intIdx;
                cb.SelectionStart = strFindStr.Length;
                cb.SelectionLength = cb.Text.Length;
                e.Handled = true;
            }
            else
            {
                e.Handled = blnLimitToList;
            }

        }
        private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            string product_property;
            try
            {
                product_property = (string)this.dataGridView1[0, e.RowIndex].Value;
            }
            catch (Exception ex)
            {
                product_property="";
            }
            
            this.combobox1.Items.Clear();
            switch (product_property)
            {
                case "status":
                    this.combobox1.Items.Add("true");
                    this.combobox1.Items.Add("false");
                    break;
                case "color":
                    this.combobox1.Items.Add("red");
                    this.combobox1.Items.Add("blue");
                    this.combobox1.Items.Add("white");
                    break;
                default:
                    return;
            }
            this.combobox1.SelectedIndex = 0;
            this.combobox1.Location = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location;
            this.combobox1.Size = this.dataGridView1.CurrentCell.Size;
            this.combobox1.Visible = true;
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (this.combobox1.SelectedItem != null)
            {
                this.dataGridView1.CurrentCell.Value = this.combobox1.SelectedItem.ToString();
            }
            this.combobox1.Visible = false;
        }
    }
}
